package com.platform.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.DeviceLogDto;
import com.platform.system.domain.entity.device.DeviceLog;
import com.platform.system.domain.vo.device.DeviceLogVo;
import com.platform.system.mapper.device.DeviceLogMapper;
import com.platform.system.service.ISysDeviceLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class SysDeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog> implements ISysDeviceLogService {

    @Resource
    DeviceLogMapper deviceLogMapper;

    @Override
    public TableDataInfo selectDeviceLogList(DeviceLogVo deviceLogVo) {
        LambdaQueryWrapper<DeviceLog> deviceLogLambdaQueryWrapper = new QueryWrapper<DeviceLog>().lambda();
        deviceLogLambdaQueryWrapper.like(StringUtils.isNotEmpty(deviceLogVo.getSn()), DeviceLog::getSn, deviceLogVo.getSn());
        deviceLogLambdaQueryWrapper.like(StringUtils.isNotEmpty(deviceLogVo.getMac()), DeviceLog::getMac, deviceLogVo.getMac());
        deviceLogLambdaQueryWrapper.orderByDesc(DeviceLog::getCreateTime);
        Page<DeviceLog> page = new Page<>();
        page.setCurrent(deviceLogVo.getPageNum());
        page.setSize(deviceLogVo.getPageSize());
        IPage<DeviceLog> deviceLogIPage = deviceLogMapper.selectPage(page, deviceLogLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<DeviceLog> list =  deviceLogIPage.getRecords();
        List<DeviceLogDto> deviceLogDtoList = new ArrayList<>();
        if(list != null || list.size() > 0){
            String array = JSONArray.toJSONString(list);
            deviceLogDtoList = JSONArray.parseArray(array, DeviceLogDto.class);
        }
        rspData.setRows(deviceLogDtoList);
        rspData.setTotal(deviceLogIPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public AjaxResult insertDeviceLog(DeviceLogVo deviceLogVo) {
        DeviceLog deviceLog = new DeviceLog();
        deviceLog.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        deviceLog.setSn(deviceLogVo.getSn());
        deviceLog.setMac(deviceLogVo.getMac());
        deviceLog.setLogUrl(deviceLogVo.getLogUrl());
        deviceLog.setUpdateTime(new Date());
        deviceLog.setCreateTime(new Date());
        deviceLog.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult emptyDeviceLogList() {
        LambdaQueryWrapper<DeviceLog> deviceLogLambdaQueryWrapper = new QueryWrapper<DeviceLog>().lambda();
        deviceLogMapper.delete(deviceLogLambdaQueryWrapper);
        return AjaxResult.success();
    }
}
